using UnityEngine;
using QFramework;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class SimpleAxe : ViewController
	{
		private float float_CreatTimer;
		void Start()
		{
			// Code Here

		}

		void Update()
		{
			float_CreatTimer += Time.deltaTime;
			if (float_CreatTimer >= 1f)
			{
				Axe.Instantiate()
				.Show()
				.Position(this.Position())
				.Self((self) =>
				{
					float PosX = RandomUtility.Choose(-8, -5, -3, 3, 5, 8);
					float PosY = RandomUtility.Choose(3, 5, 8);

					self.GetComponent<Rigidbody2D>().velocity = new Vector2(PosX, PosY);
					self.OnTriggerEnter2DEvent((colliction) =>
					{
						if (colliction.GetComponent<HurttBox>())
						{
							if (colliction == null || colliction.GetComponent<HurttBox>() == null || colliction.GetComponent<HurttBox>().owner == null) return;

							if (colliction.GetComponent<HurttBox>().owner.CompareTag("Enemy"))
								colliction.GetComponent<HurttBox>().owner.GetComponent<IEnemy>().OnSetHp(-5f);
						}
					}).UnRegisterWhenGameObjectDestroyed(self.gameObject);

					ActionKit.OnUpdate.Register(() =>
					{
						if (Player.Default)
						{
							float disY = Player.Default.transform.PositionY() - self.transform.PositionY();

							if (disY >= 15f)
							{
								self.DestroyGameObjGracefully();
							}
						}

					}).UnRegisterWhenGameObjectDestroyed(self.gameObject);
				});
				float_CreatTimer = 0;
			}
		}
	}
}
